package com.itheima.service.front.impl;

import com.itheima.dao.front.ExamPaperDao;
import com.itheima.dao.front.ExamQuestionDao;
import com.itheima.dao.store.QuestionDao;
import com.itheima.domain.front.ExamPaper;
import com.itheima.domain.front.ExamQuestion;
import com.itheima.domain.store.Question;
import com.itheima.factory.MapperFactory;
import com.itheima.service.front.ExamService;
import com.itheima.utils.TransactionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @program: mm_exam
 * @description:
 * @author: zhanghz001
 * @create: 2020-10-20 11:09
 **/
public class ExamServiceImpl implements ExamService {
    @Override
    public List<Question> getPaper() {

        SqlSession sqlSession = null;
        try {
            //1.获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            //2.获取Dao
            QuestionDao questionDao = MapperFactory.getMapper(sqlSession, QuestionDao.class);
            List<Question> questionList = questionDao.findAll();

            return questionList;
        } catch (Exception e) {
            throw new RuntimeException(e);
            //记录日志
        } finally {
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public boolean applyPaper(String memberId, List<ExamQuestion> examQuestionList) {

        SqlSession sqlSession = null;
        try {
            //1.获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            //2.获取Dao
            ExamPaperDao examPaperDao = MapperFactory.getMapper(sqlSession, ExamPaperDao.class);
            ExamQuestionDao examQuestionDao = MapperFactory.getMapper(sqlSession, ExamQuestionDao.class);
            //id使用UUID的生成策略来获取
            String paperId = UUID.randomUUID().toString();

            //3.调用Dao层操作

            boolean flag = false;
            //存储 了试卷信息
            ExamPaper examPaper = new ExamPaper();
            examPaper.setId(paperId);
            examPaper.setMemberId(memberId);
            examPaper.setApplyTime(new Date());
            examPaper.setState("1");
            int examPaperFlag = examPaperDao.save(examPaper);
            flag = examPaperFlag > 0;
            //存储了题目和对应的答案信息
            for (ExamQuestion eq : examQuestionList) {
                eq.setId(UUID.randomUUID().toString());
                eq.setExamPaperId(paperId);
                int eqFlag = examQuestionDao.save(eq);
                flag = flag && (eqFlag > 0);
            }
            //4.提交事务
            TransactionUtil.commit(sqlSession);
            return flag;
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
            //记录日志
        } finally {
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
